package cloud.demarcia.rabbitregister.util;

import cloud.demarcia.rabbitregister.constant.EncodingConstant;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.AES;
import org.springframework.stereotype.Component;

/**
 * @author Bojun Wen, wen_bj@outlook.com
 * @date 2022.02.26
 */
@Component
public class PasswordEncipher {

    private final AES encipher=new AES(
            Mode.CTS, Padding.PKCS5Padding, EncodingConstant.KEY.getBytes(), EncodingConstant.SALT.getBytes());

    public String encode(String text){
        String s = StrUtil.addPrefixIfNot(text, EncodingConstant.PREFIX);
        return encipher.encryptBase64(s);
    }

    public String decode(String text){
        String s= encipher.decryptStr(text);
        return StrUtil.removePrefix(s, EncodingConstant.PREFIX);
    }
}
